//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Model;

    internal class VerificationFactoryProxy : IVerificationFactory
    {
        private readonly IVerificationFactory verificationFactory;

        public VerificationFactoryProxy(IVerificationFactory verificationFactory)
        {
            this.verificationFactory = verificationFactory;
        }

        public ApplicationDescription ApplicationDescription
        {
            get
            {
                try
                {
                    return this.verificationFactory.ApplicationDescription;
                }
                catch (Exception exception)
                {
                    Log.LogError(
                        "An exception occured while attempted to get application description from VerificationFactory: {0}. Exception: {1}",
                        this.verificationFactory.GetType(),
                        exception);

                    return ApplicationDescription.FailingDescription;
                }
            }
        }

        public IEnumerable<IVerification> GetVerifications()
        {
            try
            {
                Log.LogVerbose("Loading Verifications from factory: {0}", this.verificationFactory.GetType());
                var verifications = this.verificationFactory.GetVerifications().ToList();
                Log.LogVerbose("Loaded: {0} verification(s). Will now return proxied verifications", verifications.Count);
                return verifications.Select(item => new VerificationProxy(item));
            }
            catch (Exception exception)
            {
                Log.LogError(
                    "Failed to get verifications from supplied factory: {0}. Will return a static verification with failure result. Exception: {1}",
                    this.verificationFactory.GetType(),
                    exception);

                return BuildStaticFailingResult(exception);
            }
        }

        private static IEnumerable<IVerification> BuildStaticFailingResult(Exception exception)
        {
            const string ErrorMessage = "Failed to get verifications from the supplied verification factory";
            return new IVerification[] { new StaticResultVerification(exception, ErrorMessage) };
        }
    }
}